package com.panghu.controller;

import com.panghu.entity.ApiResult;
import com.panghu.entity.PaperManage;
import com.panghu.service.PaperManageService;
import com.panghu.service.impl.FillQuestionServiceImpl;
import com.panghu.service.impl.JudgeQuestionServiceImpl;
import com.panghu.service.impl.MultiQuestionServiceImpl;
import com.panghu.utils.ApiResultHandler;
import com.panghu.vo.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//接受前端发送的数据 进行试卷保存
@RestController
public class ItemController {
    //选择题
    @Autowired
    MultiQuestionServiceImpl multiQuestionService;

    //填空题
    @Autowired
    FillQuestionServiceImpl fillQuestionService;

    //判断题
    @Autowired
    JudgeQuestionServiceImpl judgeQuestionService;

    //试卷管理
    @Autowired
    PaperManageService paperService;


    /**
     * 1.首先获取到题目的个数
     */
    @PostMapping("/item")
    public ApiResult ItemController(@RequestBody Item item) {
        // 选择题
        Integer changeNumber = item.getChangeNumber();
        // 填空题
        Integer fillNumber = item.getFillNumber();
        // 判断题
        Integer judgeNumber = item.getJudgeNumber();
        //出卷id
        Integer paperId = item.getPaperId();

        // 查询题目是否已经存在
        List<Integer>  changeNumbers = multiQuestionService.findBySubject(item.getSubject(), changeNumber);
        if(changeNumbers==null){
            return ApiResultHandler.buildApiResult(400,"选择题数据库获取失败",null);
        }
        for (Integer number : changeNumbers) {
            PaperManage paperManage = new PaperManage(paperId,1,number);
            boolean index = paperService.save(paperManage);
            if(!index)
                return ApiResultHandler.buildApiResult(400,"选择题组卷保存失败",null);
        }

        // 填空题
        List<Integer> fills = fillQuestionService.findBySubject(item.getSubject(), fillNumber);
        if(fills==null)
            return ApiResultHandler.buildApiResult(400,"填空题数据库获取失败",null);
        for (Integer fillNum : fills) {
            PaperManage paperManage = new PaperManage(paperId,2,fillNum);
            boolean index = paperService.save(paperManage);
            if(!index)
                return ApiResultHandler.buildApiResult(400,"填空题题组卷保存失败",null);
        }
        // 判断题
        List<Integer> judges = judgeQuestionService.findBySubject(item.getSubject(), judgeNumber);
        if(fills==null)
            return ApiResultHandler.buildApiResult(400,"判断题数据库获取失败",null);
        for (Integer judge : judges) {
            PaperManage paperManage = new PaperManage(paperId,3,judge);
            boolean index = paperService.save(paperManage);
            if(!index)
                return ApiResultHandler.buildApiResult(400,"判断题题组卷保存失败",null);
        }


        return ApiResultHandler.buildApiResult(200,"试卷组卷成功",null);
    }
}
